#pragma once

#include "doc/convert/attribute_convert/multivalue_attribute_convertor.h"

namespace hawking {
namespace indexlib {

/*
    memory layout:
    | StringLengthType(string length) | string | ......
*/
class StringAttributeConvertor : public MultiValueAttributeConvertor<char> {
public:
    StringAttributeConvertor(util::ColumnType column_type) :
        MultiValueAttributeConvertor<char>(column_type) {}
    ~StringAttributeConvertor() = default;

protected:
    virtual std::string_view InnerEncode_(
        const util::FieldValue* field_value, Pool* pool) override {
        const std::string& str = field_value->bytes_value();
        StringLengthType str_length = static_cast<StringLengthType>(str.length());
        char* begin = Allocate_(pool, str_length + sizeof(StringLengthType));
        char* buffer = begin;
        AppendStrLength_(str_length, buffer);
        std::memcpy(buffer, str.data(), str_length);
        return std::string_view(begin, str_length + sizeof(StringLengthType));
    }

    virtual void InnerDecode_(
        const uint8_t* data_addr, util::FieldValue* value) override {
        if (util::ColumnType::COLUMN_STRING != column_type_) {
            LOG(ERROR) << "Un string type, column_type_ "
                << static_cast<int32_t>(column_type_);
        }
    }
};

}
}